package com.movie.controller.MovieContorller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movie.beans.Comment;
import com.movie.beans.Movies;
import com.movie.beans.SiteRate;
import com.movie.beans.base.AjaxResult;
import com.movie.beans.dto.CommentDto;
import com.movie.beans.dto.MoviesDto;
import com.movie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/movie")
@Controller
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private SiteRateService siteRateService;
    @Autowired
    private RateService rateService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;


    @GetMapping("/{id}")
    public String index(@PathVariable(name="id") Long id, ModelMap modelMap){
        Movies movie = movieService.getMoviesById(id);
        SiteRate siteRate = siteRateService.selectSiteRateByMovieId(id);
        MoviesDto dto = new MoviesDto();

        dto.setActor("John/Rose");
        dto.setDirector("Merry");
        dto.setGenres(movie.getGenres());
        dto.setMovieId(id);
        dto.setTitle(movie.getTitle());
        if (siteRate != null) {
            dto.setPoint(siteRate.getPoint());
            dto.setSiteRate((int) (siteRate.getRate()*10));
        }
        else {
            dto.setPoint(0.0);
            dto.setSiteRate((int) 0.0);
        }
        dto.setRemark("单身貌美的方卉（白百何 饰），独自在大城市打拼，过着两点一线的生活。一天夜里，她突然听见门外有人暴力拉拽门把手试图闯入。虽然没有看清门外是谁，但通过保安小吴得知，自己楼层的监控坏了。这让方卉平静的独居生活突然笼罩在惊恐不安的阴影之中。当夜晚再次降临，熟睡中的方卉隐约察觉有人进入了她的房间，但她醒来后却没有发现任何迹象。渐渐，她的精神状态也开始变得越来越差。身边反复出现的几个男人，房屋中介高齐、保安小吴、公司领导李总、宠物医生郑飞，他们似乎都有自己不可告人的目的。随着好闺蜜乔小曼的离奇失踪，独居，几乎成为了方卉的梦魇……\n");
        Comment c = new Comment();
        c.setMovieId(id);
        PageHelper.startPage(1,5);
        List<Comment> comments = commentService.selectCommentById(c);
        List<CommentDto> commentDtos = new ArrayList<>();
        for (Comment item:comments) {
            CommentDto commentDto = new CommentDto();
            commentDto.setId(item.getId());
            commentDto.setTime(item.getTime());
            commentDto.setUserId(item.getUserId());
            commentDto.setStatus(item.getStatus());
            commentDto.setMovieId(item.getMovieId());
            commentDto.setComment(item.getComment());
            commentDto.setUserName(userService.selectUserById(item.getUserId()).getUserName());
            commentDto.setLoginName(userService.selectUserById(item.getUserId()).getLoginName());
            SiteRate siteRate1 = siteRateService.selectSiteRateByMovieId(item.getMovieId());
            if (siteRate1!=null){
                commentDto.setRate((int) (siteRate1.getRate()*10));
            }
            else {
                commentDto.setRate((int)(0.0*10));
            }
            commentDtos.add(commentDto);
        }
        modelMap.addAttribute("count",new PageInfo(comments).getTotal());
        modelMap.addAttribute("comments",commentDtos);
        modelMap.addAttribute("movie",dto);
        return "/page/movie";
    }

    @PostMapping("/delete")
    @ResponseBody
    public AjaxResult deleteMovie(Long movieId){
        Movies movies=new Movies();
        movies.setMovieId(movieId);
        int t=movieService.deleteMoviesById(movies);
        if(t>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
